using System;
using System.Collections.Generic;
using HelloWorld.Helper;

namespace HelloWorld.Learn
{
    public delegate double Function(double x);
    
    public class DelegateDemo
    {
        static double Square(double x)
        {
            return x * x;
        }

        public static double[] Apply(double[] x, Function f)
        {
            var result = new double[x.Length];

            for(int i =0; i<x.Length; i++)
                result[i] = f(x[i]);

            return result;
        }

        static void Print(double[] dd)
        {
            foreach(var d in dd) 
            {
                var x = Math.Round(d, 5);
                Console.Write($"{x,12:0.00}");
            }
            
            Console.WriteLine();
        }

        public static void Demo()
        {
            double[] a = {2.5, 3.4, 4.6, 5.7, 6.8, 7.9, 8.0};
            var a1 = Apply(a, Square);
            Print(a1);
            var a2 = Apply(a, Math.Sin);
            Print(a2);
            var circle = new Circle();
            var a3 = Apply(a, circle.Perimeter);
            Print(a3);
            var a4 = Apply(a, circle.Area);
            Print(a4);
        }
    }
}
